package com.template.core;

import java.util.ArrayList;
import java.util.List;

public class TableBean {

	private String tableName;
	private List<PrimaryBean> primarys = new ArrayList<PrimaryBean>();
	private List<ExportedKeyBean> exportedKeys = new ArrayList<ExportedKeyBean>();
	private List<ImportedKeyBean> importedKeys = new ArrayList<ImportedKeyBean>();
	private List<ColumnBean> columns = new ArrayList<ColumnBean>();

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public List<PrimaryBean> getPrimarys() {
		return primarys;
	}

	public List<ColumnBean> getColumns() {
		return columns;
	}

	public List<ExportedKeyBean> getExportedKeys() {
		return exportedKeys;
	}

	public List<ImportedKeyBean> getImportedKeys() {
		return importedKeys;
	}

	public void addColumn(ColumnBean cb) {
		isPrimary(cb);
		isExportedKey(cb);
		isImportKey(cb);
		columns.add(cb);
	}

	public void addPrimary(PrimaryBean pb) {
		primarys.add(pb);
	}

	public void addExportedKey(ExportedKeyBean ekb) {
		exportedKeys.add(ekb);
	}
	
	public void addImportedKey(ImportedKeyBean ekb) {
		importedKeys.add(ekb);
	}

	public boolean isPrimary(ColumnBean cb) {
		if (primarys != null) {
			for (PrimaryBean pb : primarys) {
				if (cb.getColumnName().equals(pb.getColumnName())) {
					pb.setDataType(cb.getDataType());
					pb.setLength(cb.getLength());
					pb.setType(cb.getType());
					cb.setPrimary(true);
					return true;
				}
			}
		}
		return false;
	}

	public boolean isExportedKey(ColumnBean cb) {
		if (exportedKeys != null) {
			for (ExportedKeyBean ekb : exportedKeys) {
				if (cb.getColumnName().equals(ekb.getFkColumnName())) {
					cb.setPkTableName(ekb.getPkTableName());
					cb.setFkTableName(ekb.getFkTableName());
					cb.setFkColumnName(ekb.getFkColumnName());
					cb.setPkColumnName(ekb.getPkColumnName());
					cb.setFk(true);
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean isImportKey(ColumnBean cb) {
		if (importedKeys != null) {
			for (ImportedKeyBean ikb : importedKeys) {
				if (cb.getColumnName().equals(ikb.getPkColumnName())) {
					cb.setPkTableName(ikb.getPkTableName());
					cb.setFkTableName(ikb.getFkTableName());
					cb.setFkColumnName(ikb.getFkColumnName());
					cb.setPkColumnName(ikb.getPkColumnName());
					cb.setPk(true);
					return true;
				}
			}
		}
		return false;
	}
}
